package com.example.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.apache.ibatis.session.SqlSessionFactory;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.SqlSessionTemplate;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.jdbc.DataSourceBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.jdbc.core.JdbcTemplate;

import javax.sql.DataSource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 对同一个接口，可能会有几种不同的实现类，而默认只会采取帶注解的，不然会报错
 * applyJdbcTemplate in com.gwssi.config.DB2DataSourceConfig required a single bean, but 2 were found
 *
 */
@Configuration
@MapperScan(basePackages = {"com.example.dao"}, sqlSessionFactoryRef = "mysqlSqlSessionFactory")
public class MysqlDataSourceConfig {

	@Bean(name = "mysqlDataSource")
    @ConfigurationProperties(prefix = "spring.datasource")
    public DataSource mysqlDataSource() {
        //指定使用DruidDataSource
       return DataSourceBuilder.create().type(DruidDataSource.class).build();
    }

    @Bean(name = "mysqlSqlSessionFactory")
    public SqlSessionFactory mySqlSessionFactory(@Qualifier("mysqlDataSource") DataSource dataSource) throws Exception {
        SqlSessionFactoryBean bean = new SqlSessionFactoryBean();
        bean.setDataSource(dataSource);
        //解决mybatis返回map时值为null字段丢失
        org.apache.ibatis.session.Configuration configuration = new org.apache.ibatis.session.Configuration();
        configuration.setCallSettersOnNulls(true);
        bean.setConfiguration(configuration);

        //必须在这里加载mybatis的xml配置文件，不能在application.properties里加载，否则不好使
//        bean.setMapperLocations(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/**/*.xml"));
        List<Resource> list = new ArrayList<>();
        list.addAll(Arrays.asList(new PathMatchingResourcePatternResolver().getResources("classpath*:mapper/*.xml")));
//        list.addAll(Arrays.asList(new PathMatchingResourcePatternResolver().getResources("classpath*:newmapper/*.xml")));

        bean.setMapperLocations(list.toArray(new Resource[list.size()]));
        return bean.getObject();
    }


//    @Bean(name = "mysqlTransactionManager")
//    public DataSourceTransactionManager mysqlTransactionManager(@Qualifier("mysqlDataSource") DataSource dataSource) {
//        return new DataSourceTransactionManager(dataSource);
//    }


    @Bean(name = "mysqlSqlSessionTemplate")
    public SqlSessionTemplate exchangeSqlSessionTemplate(@Qualifier("mysqlSqlSessionFactory") SqlSessionFactory sqlSessionFactory) throws Exception {
        return new SqlSessionTemplate(sqlSessionFactory);
    }


    @Bean(name = "mysqlJdbcTemplate")
    public JdbcTemplate mysqlJdbcTemplate(@Qualifier("mysqlDataSource") DataSource dataSource) {
        return new JdbcTemplate(dataSource);
    }
}
